// pages/shopManager/bannerManager/bannerList/bannerList.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getBannerList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            bannerList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getBannerList()
    },

    getBannerList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let bannerList = this.data.bannerList
        let data = {
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            client: 2,
            page: this.data.nowPage,
            limit: 10
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Slideshow/slideshow_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    bannerList: bannerList,
                    isAll: true
                })
            } else {
                bannerList = bannerList.concat(res)
                that.setData({
                    bannerList: bannerList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    goAddBanner: function() {
        wx.navigateTo({
            url: '/pages/shopManager/bannerManager/bannerEdit/bannerEdit'
        })
    },

    goEdit: function(e) {
        wx.navigateTo({
            url: '/pages/shopManager/bannerManager/bannerEdit/bannerEdit?bannerId=' + e.currentTarget.dataset.id
        })
    },

    goDelete: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认删除轮播图？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    let data = {
                        id: e.currentTarget.dataset.id
                    }
                    common.post('Slideshow/delete_slideshow', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '删除轮播图成功',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    that.setData({
                                        bannerList: [],
                                        nowPage: 0,
                                        isLoading: false,
                                        isAll: false
                                    })
                                    that.getBannerList()
                                }
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    },

    updateData: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            client: 2,
            page: 0,
            limit: this.data.nowPage * 10
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Slideshow/slideshow_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    bannerList: []
                })
            } else {
                that.setData({
                    bannerList: res
                })
            }
        }).catch(function(e) {})
    }
})